<?php
/**
 * Created by lihuan.
 * User: lihuan
 * Date: 2018/3/29 0029
 * Time: 下午 2:43
 */
namespace app\wxmsg\controller;

use app\wxmsg\controller\TextMsg;
use app\wxmsg\controller\EventMsg;

include_once 'vendor/encrypt/wxBizMsgCrypt.php';

class Index
{
    //微信服务器访问入口地址
    public function index()
    {
        logInfo("new enter");
        if (! isset($_GET['echostr'])) {
            // 事件处理
            if ($this->checkSignature()) {
                logInfo("start");
                // 签名正确才进行事件处理
                $this->responseMsg();
            }
        } else {
            // 签名验证
            $this->valid();
        }
    }
    
    // 签名验证
    private function valid()
    {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
        } else {
            echo 0;
        }
    }

    private function responseMsg()
    {
        // get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET["nonce"];
        $msg_signature = $_GET['msg_signature'];
        $encrypt_type = $_GET['encrypt_type'];
        
        $token = config("weixinConfig")["token"];
        $EncodingAESKey = config("weixinConfig")["EncodingAESKey"];
        $appId = config("weixinConfig")["appId"];
        // 创建加解密对象
        $pc = new \WXBizMsgCrypt($token, $EncodingAESKey, $appId);
        
        // 对微信发送过来的信息进行解密
        $decryptMsg = '';
        $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $postStr, $decryptMsg);
        $postStr = $decryptMsg;
        
        logInfo("postStr:".$postStr);
        
        if (! empty($postStr)) {
            
            // 解析xml格式
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            
            $msgType = trim($postObj->MsgType);

            switch ($msgType) {
                case "text":
                    $textMsg = new TextMsg();
                    $resultStr = $textMsg->handelMsg($postObj);
                    break;
                case "event":
                    $eventMsg = new EventMsg();
                    $resultStr = $eventMsg->handelMsg($postObj);
                    break;
                default:
                    $resultStr = "Unknow msg type: " . $msgType;
                    break;
            }
            echo $resultStr;
        } else {
            echo "";
        }
    }

    //签名，按照微信的签名方式计算即可。
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        
        $token = config("weixinConfig")["token"];
        $tmpArr = array(
            $token,
            $timestamp,
            $nonce
        );
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }
}  